<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Form\BackActivity;
use App\Admin\Repositories\ActivityAreaSetting;
use App\Models\Activity;
use App\Models\ActivityAreaSetting as AppActivityAreaSetting;
use App\Services\TreeService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Cache;

class ActivityAreaSettingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ActivityAreaSetting(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->column('id')->sortable();
            // $grid->column('act_id');
            // $grid->column('img');
            $grid->column('title')->tree();
            // $grid->column('parent_id');
            // $grid->column('order');
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $activity_id = request()->input('act_id');
            if (!empty($activity_id) || isset($activity_id)) {
                $grid->model()->where('act_id', $activity_id);
            }
            $grid->tools('<a href="/admin/activity/area/create?act_id=' . $activity_id . '"  class="btn btn-primary btn-outline">
    <i class="feather icon-plus"></i><span class="d-none d-sm-inline">&nbsp;&nbsp;新增</span>
</a>');
            $grid->tools('<a href="/admin/activity/list" class="btn btn-primary dialog-create">返回</a>');
            $grid->actions(function (Grid\Displayers\Actions $actions) use ($activity_id) {
                $actions->append('<a style="cursor: pointer;" class="act-aMJZRoRIMdfwCxQ2" href="/admin/activity/area/' . $this->id . '/edit?act_id=' . $activity_id . '"><i class="feather icon-edit-1 text-custom"></i> <span class="text-custom">编辑</span> &emsp;</a>');
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ActivityAreaSetting(), function (Show $show) {
            $show->field('id');
            $show->field('act_id');
            $show->field('img');
            $show->field('title');
            $show->field('parent_id');
            $show->field('order');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ActivityAreaSetting(), function (Form $form) {

            $form->display('id');

            $form->disableViewButton();
            $form->disableViewCheck();
            $form->disableListButton();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();

            $form->tools(function (Form\Tools $tools) {
                // 添加一个按钮, 参数可以是字符串, 匿名函数, 或者实现了Renderable或Htmlable接口的对象实例
                $tools->append(new BackActivity());
                $tools->append('<a href="javascript:history.back(-1)" class="btn btn-primary dialog-create">返回上一级</a>');
            });

            $activity_id = request()->input('act_id');
            // dump($activity_id);
            Cache::add('act_id_area', $activity_id);

            $array = AppActivityAreaSetting::where('act_id', $activity_id)->get();
            $array->makeHidden('updated_at');
            $array = $array->toArray();
            $trees = TreeService::toListByKey($array);

            // $form->select('act_id')->options(Activity::where('status', 1)->where('is_del', 0)->pluck('activity_name', 'id'));

            $form->hidden('act_id');
            $form->select('parent_id', trans('admin.parent_id'))
                ->options($trees)
                ->saving(function ($v) {
                    return (int) $v;
                })->default(0)->required();

            $form->text('title')->required();
            $form->text('order')->default(0);

            if ($form->isCreating()) {

                $form->saving(function (Form $form) {

                    $act_id = Cache::get('act_id_area');

                    $form->act_id = $act_id;
                });
            }

            if ($form->isEditing()) {
                $form->saving(function (Form $form) {
                    $act_id = Cache::get('act_id_area');
                    $form->act_id = $act_id;
                });
            }

            // 跳转并提示成功信息
            $form->saved(function (Form $form) {
                $url = request()->input('_previous_');
                $query = parse_url($url);
                $arr = explode('=', isset($query['query']) ? $query['query'] : '');
                Cache::pull('act_id_area');
                return $form->response()->success('保存成功')->redirect('/activity/area?act_id=' . $arr[1]);
            });
        });
    }
}
